package com.qlmjx.sevalopos.View;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.qlmjx.sevalopos.Bean.CustomerGoodsBean;
import com.qlmjx.sevalopos.Bean.ManageGoodInfo;
import com.qlmjx.sevalopos.Bean.NetGoodsBean;
import com.qlmjx.sevalopos.R;
import com.qlmjx.sevalopos.adapter.DialogSaleAdapter;
import com.qlmjx.sevalopos.event.ShopEvent;
import com.qlmjx.sevalopos.https.HttpAddress;
import com.qlmjx.sevalopos.https.RequestParams;
import com.qlmjx.sevalopos.utils.ConstantsUtils;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;
import com.universal.frame.pulltorefresh.PullToRefreshBase;
import com.universal.frame.pulltorefresh.PullToRefreshScrollView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import okhttp3.Request;


/**
 * 退出登录
 * @author Administrator
 */
public class ShopOrderDialog extends BaseDialog  implements PullToRefreshBase.OnRefreshListener2, ResultCallBack, AdapterView.OnItemClickListener {

	private  double one;
	private  double two;
	private PullToRefreshScrollView pull_refresh_scrollview;
	private NoScrollingListView nlv_goods;
	private String text;
	private int type;
	private DialogSaleAdapter saleAdapter;
	private int page =1;
	private List<NetGoodsBean> list = new ArrayList<>();
	private boolean toRefresh= true;

	public ShopOrderDialog(Context context) {
		super(context);
		this.context = context;
	}

	public ShopOrderDialog(Context context, String text,int type,double one,double two,int theme, LeaveMyDialogListener listener) {
		super(context, theme,listener);
		this.context = context;
		this.listener = listener;
		this.text = text;
		this.type = type;
		this.one = one;
		this.two = two;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog_shop_order);
		TextView tv_content = (TextView) findViewById(R.id.tv_content);
		tv_content.setText("请选择商品");
		pull_refresh_scrollview = findViewById(R.id.pull_refresh_scrollview);
		nlv_goods =findViewById(R.id.nlv_goods);
		initScrollView();
		getShopInfo(text);
		nlv_goods.setOnItemClickListener(this);
	}
	public void initScrollView() {
		this.pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
				.setPullLabel(
						context.getResources().getString(
								R.string.pull_to_refresh_pull_label_up));
		pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
				.setRefreshingLabel(
						context.getResources().getString(
								R.string.pull_to_refresh_refreshing_label));
		pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
				.setReleaseLabel(
						context.getResources().getString(
								R.string.pull_to_refresh_release_label));

		pull_refresh_scrollview.setMode(PullToRefreshBase.Mode.DISABLED);//both  可以上拉、可以下拉
		pull_refresh_scrollview.setOnRefreshListener(this);
		saleAdapter = new DialogSaleAdapter(context, list);
		nlv_goods.setAdapter(saleAdapter);

//		lv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				Intent intent = new Intent(getActivity(), StoreDetailsActivity.class);
//				intent.putExtra("SHOP_ID", list.get(position).id);
//				startActivity(intent);
//			}
//		});
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase refreshView) {
		pull_refresh_scrollview.onRefreshComplete();
		page = 1;
		getShopInfo(text);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase refreshView) {
		pull_refresh_scrollview.onRefreshComplete();
		if (toRefresh) {
			page++;
			getShopInfo(text);
		} else {
			ToastUtil.showToast(getContext(), "没有更多数据");
			pull_refresh_scrollview.onRefreshComplete();
		}
	}
	public void getShopInfo(String content) {
		String userID = SharedPreferencesUtil.getString(context, ConstantsUtils.USER_ID);
		String compayID = SharedPreferencesUtil.getString(context, ConstantsUtils.COMPANY_ID);
		OKHttpUtils.postAsync(context
				, HttpAddress.SERVICE_LOCATIONMEUM, userID
				, new RequestParams(context).getShopParams(compayID, content)
				, this
				, false, 1);
	}

	@Override
	public void onSuccess(String response, int flag) {
		CustomerGoodsBean bean = new CustomerGoodsBean();
		ManageGoodInfo info = JSON.parseObject(response, ManageGoodInfo.class);
		if (info.list != null) {
			if (info.list.size() > 0) {

				if (page == 1) {
					list.clear();
					list.addAll(info.list);

				} else {
					list.addAll(info.list);
				}
			} else {
				list.clear();
			}
//			if (info.list.size() < 30) {
//				toRefresh = false;
//			} else {
//				toRefresh = true;
//			}

			saleAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onFailure(Request request, IOException e, int flag) {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		CustomerGoodsBean bean = new CustomerGoodsBean();
		bean.ShopsysId= list.get(position).PART_ID;
		bean.ShopId = list.get(position).PART_NO;
		bean.ShopName = list.get(position).PART_NAME;
		bean.ShopDiscount = list.get(position).PART_CAT;
		bean.ShopNum = 1;
		if (type == 0||type == 1) {
			bean.ShopPrice = list.get(position).SALE_PRICE;
			bean.ShopSalePrice = bean.ShopPrice;
			bean.ShopMemberPrice = bean.ShopPrice;
			bean.ShopTotal = bean.ShopPrice;
		} else {
			if ("1".equals(list.get(position).PART_CAT)) {
				bean.ShopPrice = list.get(position).SALE_PRICE;
				bean.ShopSalePrice = convertToDouble(list.get(position).SALE_PRICE, 0.00) * one + "";
				bean.ShopMemberPrice = bean.ShopSalePrice;
				bean.ShopTotal = bean.ShopPrice;
			} else {
				bean.ShopPrice =list.get(position).SALE_PRICE;
				bean.ShopSalePrice = convertToDouble(list.get(position).SALE_PRICE, 0.00) * two + "";
				bean.ShopMemberPrice = bean.ShopSalePrice;
				bean.ShopTotal = bean.ShopPrice;
			}
		}
		EventBus.getDefault().post(new ShopEvent(bean));
	}
	//把String转化为double
	public static double convertToDouble(String number, double defaultValue) {
		if (TextUtils.isEmpty(number)) {
			return defaultValue;
		}
		try {
			return Double.parseDouble(number);
		} catch (Exception e) {
			return defaultValue;
		}

	}
}